package com.xiaoshuidi.cloud.module.iot.controller.admin.callback;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.YunDingDeviceCallbackReqVO;
import com.xiaoshuidi.cloud.module.iot.dal.model.callback.YunDingDeviceEventReqVO;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.service.YunDingCallbackService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Sky
 * create 2019/06/18
 * email sky.li@ixiaoshuidi.com
 **/
@RestController
@Tag(name = "智能设备 - 云丁回调")
@RequestMapping(ApiConstants.NAME + "/callback/yunding")
@Slf4j
public class YunDingDeviceController {

    private Logger LOGGER = LoggerFactory.getLogger(YunDingDeviceController.class);

    @Autowired
    private YunDingCallbackService yunDingCallbackService;

    /**
     * 云丁事件处理
     *
     * @param
     * @return
     */
    @PostMapping("event")
    public void event(@RequestBody YunDingDeviceEventReqVO yunDingDeviceEventReqVO) {
        LOGGER.info("云丁事件请求，请求内容:{}", JSONObject.toJSONString(yunDingDeviceEventReqVO));
        try {
            yunDingCallbackService.disposeEvent(yunDingDeviceEventReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("云丁事件请求,请求内容：{},errormesage:{}", JSONObject.toJSONString(yunDingDeviceEventReqVO), e);
        }
    }

    /**
     * 云丁回调处理
     *
     * @param
     * @return
     */
    @PostMapping("cb")
    public void callback(@RequestBody YunDingDeviceCallbackReqVO yunDingDeviceCallbackReqVO) {
        LOGGER.info("云丁回调请求，请求内容:{}", JSONObject.toJSONString(yunDingDeviceCallbackReqVO));
        try {
            yunDingCallbackService.disposeCallback(yunDingDeviceCallbackReqVO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("云丁回调请求,请求内容：{},errormesage:{}", JSONObject.toJSONString(yunDingDeviceCallbackReqVO), e);
        }
    }

}
